package com.service.calculate.hookArmCase;

import java.util.List;
import java.util.concurrent.PriorityBlockingQueue;

import com.service.bean.alarm.HookArmAlarm;
import com.service.bean.device.HookArmCase;
import com.service.bean.point.LabelPoint;
import com.service.bean.signal.GpsHookArmCaseSignal;
import com.service.cache.CacheManager;
import com.service.other.Logger;
import com.service.other.ServerConfig;
import com.service.tools.Tools;

/**
 * 勾臂车箱体 计算线程
 * @author lj
 *
 */
public class HookArmScalarCal implements Runnable {

	@Override
	public void run() {
		try {
			// 根据线程名获取定位数据队列
			PriorityBlockingQueue<GpsHookArmCaseSignal> signalQueue = 
					CacheManager.instance().getHookArmSignalQueue(Thread.currentThread().getName());
			GpsHookArmCaseSignal signal = null;
			HookArmCase box = null;
			while (null != signalQueue.peek()) {
				// 获取计算后的定位数据
				signal = signalQueue.poll();
				// 获取勾臂车箱体对象
				box = CacheManager.instance().getHookArmCase(Thread.currentThread().getName());
				//修改满溢报警位
				setIsGarbageFull(box,signal);
				// 将计算后的定位数据存入对应的队列，准备存入数据库
				CacheManager.instance().getHookArmCaseSignalQueue(box.company).offer(signal);
				// 计算报警信息
				calAlarm(box, signal);
				// 更新缓存中最后一条定位数据
				box.lastGps = signal;
			}
		} catch (Exception e) {
			Logger.err(e);
		}
	}
	
	/**
	 * 计算是否有满溢报警</br>
	 * 该报警只报第一条报警, 若是连续报警, 仅将第一条报警提供给前端页面, 但是仍旧插入数据库
	 * @param box
	 * @param signal
	 */
	private void calAlarm(HookArmCase box, GpsHookArmCaseSignal signal) {
		if (0 == signal.isGarbageFull) {
			// 箱体未满溢
			box.lastAlarm = null;
			return;
		}
		HookArmAlarm alarm = new HookArmAlarm();
		alarm.signalId = signal.signalId;
		alarm.gpsNo = signal.gpsNo;
		alarm.caseId = signal.caseId;
		alarm.caseName = signal.caseName;
		alarm.alarmTime = signal.gpsTime;
		alarm.baiduLng = signal.baiduLng;
		alarm.baiduLat = signal.baiduLat;
		alarm.companyCode = signal.companyCode;
		if (null != box.lastAlarm && 1 == box.lastGps.isGarbageFull) {
			/*
			 * 连续满溢
			 */
			alarm.keepTime = (signal.gpsTime.getTime() - box.lastAlarm.alarmTime.getTime()) / 1000;
			alarm.firstAlarmTime = box.lastAlarm.firstAlarmTime;
		} else {
			/*
			 * 第一次满溢
			 */
			alarm.keepTime = 0;
			alarm.firstAlarmTime = signal.gpsTime;
		}
		// 更新报警
		box.lastAlarm = alarm;
		if (null != alarm)
			CacheManager.instance().getHookArmAlarmSaveQueue(box.company).offer(alarm);
	}

	/**
	 * 修改满溢报警位
	 * @param box
	 * @param signal
	 */
	private void setIsGarbageFull(HookArmCase box, GpsHookArmCaseSignal signal) {
		if (0 == signal.batteryStatus && 0 == signal.isGarbageFull && box.lastGps != null
				&& 1 == box.lastGps.isGarbageFull) {
			boolean isSet = false;
			// 获取车辆垃圾处理场坐标点
			List<LabelPoint> gdsPoints = box.garbageDisposalSitePoints;
			for (LabelPoint point : gdsPoints) {
				double a = Tools.getTwoPointDistance(point.baiduLng, point.baiduLat, signal.baiduLng, signal.baiduLat);
				if (a <= ServerConfig.instance().garbageDisposalSiteDistance) {
					isSet = true;
					break;
				}
			}
			if (!isSet) {
				signal.isGarbageFull = 1;
			}
		}
	}
}
